"""
中信接口测试
"""
import os
import sys
import django

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()

from core.yl.client import yl_app_pay, YLClient
from core.yl.models import YLPayRecord, YLRefundRecord
from apps.czb.models import Orders
from apps.supply_order.models import RefundRecord

yl_client = YLClient()


def search():
    # response = yl_app_pay.search("319420073115235457599325")
    # print(response, "response")

    response = YLClient().query("101720040510584188089681")
    print(response)


def refund():
    orderSn = "CZBA315260021941901"
    out_trade_no = "319420073116123297090812"
    out_refund_no = yl_client.unique_value
    orders_instance = Orders.objects.get(orderSn=orderSn)
    pay_record_obj = YLPayRecord.objects.get(out_trade_no=out_trade_no, is_pay=True)
    refundAmount = pay_record_obj.totalAmount
    status, message = YLRefundRecord.objects.yl_refund(refundAmount, pay_record_obj.out_trade_no,
                                                       refundOrderId=out_refund_no,
                                                       pay_type=orders_instance.pay_type)
    print(status, message, "退款")
    refund_instance = RefundRecord.objects.create_refund(
        refund_fee=refundAmount,
        refund_type=orders_instance.pay_type,
        out_refund_no=out_refund_no
    )
    orders_instance.refund = refund_instance
    orders_instance.result = 2
    orders_instance.save()
    content = {"code": 200, "message": "OK", "result": {}}
    print(content, "车主邦退款异步通知，返回数据")


if __name__ == '__main__':
    refund()
